package com.g2room.myuall.grap.ky;

import java.io.IOException;
import java.net.MalformedURLException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.g2room.driving.grap.GenericGrap;
import com.g2room.myuall.grap.ky.model.po.GrapErr;
import com.g2room.myuall.grap.ky.model.po.PeeGao;
import com.g2room.myuall.util.DbUtil;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;

public class GrapKyGao {
//	考研kaoyan.com
//	留学http://www.liuxue86.com/
//	论文http://biyelunwen.yjbys.com/
	private static final String source="考研帮"; 
	GrapKyGao() {}
	public static void main(String[] args) throws Exception{
		new GrapKyGao().getListGao();
	}
	
	/**
	 * 获取分页列表
	 * @param url
	 * @throws FailingHttpStatusCodeException
	 * @throws MalformedURLException
	 * @throws IOException
	 */
	public void getListGao() {
		String url = "http://www.kaoyan.com/kaoyan/11/538136/";
		List<PeeGao> pgs = new ArrayList<PeeGao>();
		// 获取分页信息
		Document doc = null;
		try {
			doc = GenericGrap.getDocByIp(url);
		} catch (IOException e) {
			System.out.println(url);
			e.printStackTrace();
			GrapErr ge = new  GrapErr();
			ge.setUrl(url);
			ge.setType("1");
			ge.setDescription("获取List错误");
			insert2ErrDb(ge);
		}
		if(doc == null) return;
		Elements trEls = doc.getElementsByTag("table").first().getElementsByTag("tr");
		String area = "";
		for(Element trEl : trEls) {
			Elements tdEls = trEl.getElementsByTag("td");
			if(tdEls.size() < 3) continue;
			if(tdEls.size() == 5) {
				area = tdEls.get(0).text();
			}
			int i =0;
			for(Element tdEl : tdEls) {
				if (i == 0) {
					i++;
					continue;
				}
				PeeGao pg = new  PeeGao();
				pg.setSource(source);
				pg.setArea(area);
				pg.setProvinceName(tdEl.text());
				Elements aEls = tdEl.getElementsByTag("a");
				if(aEls.size() > 0) {
					pg.setSourceUrl(aEls.first().attr("href"));
					pgs.add(pg);
				}
			}
		}
        for(PeeGao pg : pgs) {
        	getDetailGao(pg);
        }
	}
	
	/**
	 * 获取详情联系方式
	 */
	public boolean getDetailGao(PeeGao pg) {
		Document doc = null;
		try {
			doc = GenericGrap.getDocByIp(pg.getSourceUrl());
		} catch (IOException e) {
//			e.printStackTrace();
			System.out.println(pg.getSourceUrl());
			GrapErr ge = new  GrapErr();
			ge.setUrl(pg.getSourceUrl());
			ge.setType("2");
			ge.setDescription("获取Detail Gao错误");
			insert2ErrDb(ge);
		}
		if(doc == null) return false;
		
		Elements trEls = doc.getElementsByTag("table").first().getElementsByTag("tr");
		int i =  0;
		for(Element trEl : trEls) {
			if (i == 0) {
				i++;
				continue;
			}
			Elements tdEls = trEl.getElementsByTag("td");
			if(tdEls.size() == 0) continue;
			pg.setSchoolName(tdEls.get(0).text());
			String content = tdEls.get(1).html();
			String contentHtml = tdEls.get(1).child(0).html();
			pg.setContent(content);
			String[] contacts = contentHtml.split("<br>");
			for(String str : contacts) {
				str = str.trim().replaceAll("[\\t\\n\\r]", "");
				if(str.startsWith("电话") || str.startsWith("联系电话")) {
					pg.setPhone(str);
				} else if (str.startsWith("地址") || str.contains("通讯地址") || str.contains("地 址")) {
					pg.setAddress(str);
				} else if (str.startsWith("邮编")) {
					pg.setZipCode(str);
				} else if (str.startsWith("邮箱") || str.contains("Email")) {
					pg.setEmail(str);
				} else if (str.startsWith("传真")) {
					pg.setFax(str);
				} else if (str.contains("联系方式") && str.contains("href")) {
					// 获取院系联系方式
					pg.setFaculty(getDetailGaoYuanxi(str));
				}
			}
			insert2GaoDb(pg);
			pg.setAddress(null);
			pg.setContent(null);
			pg.setSchoolName(null);
			pg.setPhone(null);
			pg.setZipCode(null);
			pg.setEmail(null);
			pg.setFax(null);
			pg.setFaculty(null);
		}
        return true;
	}
	
	public String getDetailGaoYuanxi(String str) {
		String url = "";
		Document doc = null;
		try {
			doc = GenericGrap.getDocByHtml(str);
			Element el = doc.getElementsByTag("a").first();
			url = el.attr("href");
			doc = GenericGrap.getDocByIp(url);
		} catch (Exception e) {
			System.out.println(url);
//			e.printStackTrace();
			GrapErr ge = new  GrapErr();
			ge.setUrl(url);
			ge.setType("3");
			ge.setContent(str);
			ge.setDescription("获取院系联系方式错误");
			insert2ErrDb(ge);
		}
		if(doc == null) return null;
		Elements aels = doc.getElementsByTag("table");
		return aels.size() > 0 ? aels.first().outerHtml() : null;
	}
	

	public static void insert2GaoDb(PeeGao pg) {
		try {
			PreparedStatement ps = DbUtil.getConn().prepareStatement("insert into pee_gao(province_name,source_url,address,area,email,source,phone,zip_code,faculty,fax,content,school_name) values(?,?,?,?,?,?,?,?,?,?,?,?)");
			//province_name,source_url,address,area,email,source,phone,zip_code,faculty,fax,content,school_name
			ps.setString(1, pg.getProvinceName());
			ps.setString(2, pg.getSourceUrl());
			ps.setString(3, pg.getAddress());
			ps.setString(4, pg.getArea());
			ps.setString(5, pg.getEmail());
			ps.setString(6, pg.getSource());
			ps.setString(7, pg.getPhone());
			ps.setString(8, pg.getZipCode());
			ps.setString(9, pg.getFaculty());
			ps.setString(10, pg.getFax());
			ps.setString(11, pg.getContent());
			ps.setString(12, pg.getSchoolName());
			ps.execute();
			
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public void insert2ErrDb(GrapErr ge) {
		try {
			PreparedStatement grapPs = DbUtil.getConn().prepareStatement("insert into grap_err(description,url,content,type) values(?,?,?,?)");
			grapPs.setString(1, ge.getDescription());
			grapPs.setString(2, ge.getUrl());
			grapPs.setString(3, ge.getContent());
			grapPs.setString(4, ge.getType());
			grapPs.execute();
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public static Map<String, Integer> getErrs() {
		Map<String, Integer> errMap = new HashMap<String, Integer>();
		try {
			Connection connection = DbUtil.getConn();
			Statement statement = connection.createStatement();
			String sql = "select url,type from univ_err where type=2";
			ResultSet rs = statement.executeQuery(sql);
			while(rs.next()) {
				errMap.put(rs.getString("url"), rs.getInt("type"));
			}
			rs.close();
			statement.close();
		} catch(Exception e) {
			e.printStackTrace();
		}
		return errMap;
	}
}
